import requests
from config import OLLAMA_API_KEY
import logging

# 设置日志
logger = logging.getLogger(__name__)

def generate_story(plot, characters):
    try:
        headers = {"Authorization": f"Bearer {OLLAMA_API_KEY}"}
        payload = {"plot": plot, "characters": characters}
        response = requests.post("https://api.ollama.com/generate_story", headers=headers, json=payload)
        response.raise_for_status()

        story = response.json()
        return story['text']
    except requests.exceptions.RequestException as e:
        logger.error(f"Story generation failed: {e}")
        return None

# 调用函数
if __name__ == "__main__":
    generated_plot = "这是一个示例情节"
    story_text = generate_story(generated_plot, ["Alice", "Bob"])
    if story_text:
        logger.info(f"Generated story: {story_text}")
